/**
 * 获取当页过滤项
 */
export function getFilterItem (data, key) {
  return Array.from(new Set((data || []).map(({ [key]: value }) => value))).map(value => ({ value, text: value }))
}

// 生成唯一id
export function guid () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    const r = (Math.random() * 16) | 0,
      v = c == 'x' ? r : (r & 0x3) | 0x8
    return v.toString(16)
  })
}

/**
 * 格式化数字为中文环境的友好格式
 * @param {Number} num 待格式化数字
 * @param {Intl.NumberFormatOptions} options 格式化选项
 */
export function currency (num, options = {}) {
  if (!num && num !== 0) {
    return ''
  }
  return new Intl.NumberFormat('zh-Hans', {
    maximumFractionDigits: 4,
    minimumFractionDigits: 4,
    notation: 'compact',
    ...options,
  }).format(num)
}

/**
 * 2023年12月12日 转 2023-12-12
 * 判断下是否有效
 */
export function stringTimeToMoment (_time) {
  let isValidTime = time => {
    // 判断识别的时间是否符合 2020-12-12 return Boolean
    const r = time.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/)
    if (r == null) return false
    const d = new Date(r[1], r[3] - 1, r[4])
    return d.getFullYear() == r[1] && d.getMonth() + 1 == r[3] && d.getDate() == r[4]
  }
  try {
    let time = _time.replace(/[^du\d]/g, '-')
    // 去掉最后一位
    return isValidTime(time.slice(0, time.length - 1)) ? time.slice(0, time.length - 1) : ''
  } catch (e) {
    return ''
  }
}

export function divider (str = '', devider = ' | ') {
  return str.replace(/,/g, devider)
}

// 数值转大写
export function changeNumMoneyToChinese (money) {
  var cnNums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] //汉字的数字
  var cnIntRadice = ['', '拾', '佰', '仟'] //基本单位
  var cnIntUnits = ['', '万', '亿', '兆'] //对应整数部分扩展单位
  var cnDecUnits = ['角', '分', '毫', '厘'] //对应小数部分单位
  var cnInteger = '整' //整数金额时后面跟的字符
  var cnIntLast = '元' //整型完以后的单位
  var maxNum = 999999999999999.9999 //最大处理的数字
  var IntegerNum //金额整数部分
  var DecimalNum //金额小数部分
  var ChineseStr = '' //输出的中文金额字符串
  var parts //分离金额后用的数组，预定义
  var Symbol = '' //正负值标记
  if (money == '') {
    return ''
  }

  money = parseFloat(money)
  if (money >= maxNum) {
    alert('超出最大处理数字')
    return ''
  }
  if (money == 0) {
    ChineseStr = cnNums[0] + cnIntLast + cnInteger
    return ChineseStr
  }
  if (money < 0) {
    money = -money
    Symbol = '负 '
  }
  money = money.toString() //转换为字符串
  if (money.indexOf('.') == -1) {
    IntegerNum = money
    DecimalNum = ''
  } else {
    parts = money.split('.')
    IntegerNum = parts[0]
    DecimalNum = parts[1].substr(0, 4)
  }
  if (parseInt(IntegerNum, 10) > 0) {
    //获取整型部分转换
    var zeroCount = 0
    var IntLen = IntegerNum.length
    for (var i = 0; i < IntLen; i++) {
      var n = IntegerNum.substr(i, 1)
      var p = IntLen - i - 1
      var q = p / 4
      var m = p % 4
      if (n == '0') {
        zeroCount++
      } else {
        if (zeroCount > 0) {
          ChineseStr += cnNums[0]
        }
        zeroCount = 0 //归零
        ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m]
      }
      if (m == 0 && zeroCount < 4) {
        ChineseStr += cnIntUnits[q]
      }
    }
    ChineseStr += cnIntLast
    //整型部分处理完毕
  }
  if (DecimalNum != '') {
    //小数部分
    var decLen = DecimalNum.length
    for (let i = 0; i < decLen; i++) {
      let n = DecimalNum.substr(i, 1)
      if (n != '0') {
        ChineseStr += cnNums[Number(n)] + cnDecUnits[i]
      }
    }
  }
  if (ChineseStr == '') {
    ChineseStr += cnNums[0] + cnIntLast + cnInteger
  } else if (DecimalNum == '') {
    ChineseStr += cnInteger
  }
  ChineseStr = Symbol + ChineseStr

  return ChineseStr
}
